package me.zhengjie.modules.echarts.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.config.R;
import me.zhengjie.modules.detailed.domain.Detailed;
import me.zhengjie.modules.detailed.service.DetailedService;
import me.zhengjie.modules.worth.domain.Capital;
import me.zhengjie.modules.worth.service.CapitalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("echarts")
public class EchartsConrtroller {


    @Autowired
    private DetailedService detailedService;

    @Autowired
    private CapitalService capitalService;

    @GetMapping("getdata/{username}")
    public R echartsgetdata(@PathVariable String username){

        //封装X轴，月份
        String today= DateUtil.today();
        Date b=DateUtil.lastMonth();
        String b1=b.toString();
        List<String> Xlist=new ArrayList<>();
        Xlist.add(today.substring(0,today.length()-3));
        Xlist.add(b1.substring(0,b1.length()-12));
        for (int i = 1; i < 6; i++) {
            Date c=DateUtil.offsetMonth(b,-i);
            String c1=c.toString();
            Xlist.add(c1.substring(0,c1.length()-12));
        }
        Collections.reverse(Xlist);

        //柱状图数据
        List <Integer> Incomelist=new ArrayList<>();
        List <Integer> Outcomelist=new ArrayList<>();
        for(String month:Xlist){
            //收入
            QueryWrapper<Detailed> wrapper1=new QueryWrapper<>();
            wrapper1.like("gmt_create",month);
            wrapper1.eq("type",1);
            wrapper1.eq("username",username);
            List<Detailed> detailedincome = detailedService.list(wrapper1);
            int income =0;
            for(Detailed de:detailedincome){
                income +=Integer.parseInt(de.getNum());
            }
            Incomelist.add(income);
            //支出
            QueryWrapper<Detailed> wrapper2=new QueryWrapper<>();
            wrapper2.like("gmt_create",month);
            wrapper2.eq("type",2);
            wrapper2.eq("username",username);
            List<Detailed> detailedoutcome = detailedService.list(wrapper2);
            int outcome =0;
            for(Detailed de:detailedoutcome){
                outcome +=Integer.parseInt(de.getNum());
            }
            Outcomelist.add(outcome);
        }

        //饼图数据
        int intotal=0;
        for(int in:Incomelist){
            intotal +=in;
        }
        int outtotal=0;
        for(int out:Outcomelist){
            outtotal +=out;
        }

        List<Double> capList=new ArrayList<>();
        for(String month:Xlist){
            QueryWrapper<Capital> wrapper =new QueryWrapper<>();
            wrapper.like("gmt_create",month);
            wrapper.eq("username",username);
            List<Capital> capitalList = capitalService.list(wrapper);

            double capitaltotal=0;
            for(Capital capital:capitalList){
                String percentage=capital.getLossrate();
                percentage = percentage.replace("%","");
                float f = Float.valueOf(percentage) / 100;

                capitaltotal +=NumberUtil.mul(Integer.parseInt(capital.getPurchasenum()),f);
            }
            capList.add(capitaltotal);
        }


        return  R.success()
                .data("income",Incomelist)
                .data("outcome",Outcomelist)
                .data("Xlist",Xlist)
                .data("intotal",intotal)
                .data("outtotal",outtotal)
                .data("capList",capList);
    }

}
